<?php
namespace Home\Controller;
use Think\Controller;
use Think\Verify;
class IndexController extends Controller {
    public function index(){
    	header("Content-Type: text/html;charset=utf-8");

        $T=M('setting');
    	$tdata=$T->where('id=1')->find();
        if($tdata['status']==0)
        {
            echo'系统正在升级中，请稍候再访问，为此对您造成不便，敬请原谅！';
            die();
        }

        $username=I('get.u');
        
        $this->assign('username',$username);


        $this->display();
    }

    public function login(){
    	header("Content-Type: text/html;charset=utf-8");
    	$username=I('post.username');
        $password=I('post.password');
        if(empty($username))
        {
        	echo"<script>alert('请输入账号！');location.href='/?u=".$username."'</script>";
            exit;

        }
        if(empty($password))
        {
        	echo"<script>alert('请输入密码！');location.href='/?u=".$username."'</script>";
            exit;

        }
        //检测验证码
        $code = I('post.code');
        if(!$this->check_verify($code))
        {
            echo"<script>alert('输入的验证码有误，请点击验证码刷新再输入！');location.href='/?u=".$username."'</script>";
            exit;
        }

        if(!empty($username)){

            $data['username'] = $username;
            $info = M('account')->where(array('account'=>$username))->find();
            if(empty($info)){
                echo"<script>alert('会员帐户不存在，请重新输入');location.href='/'</script>";
                die();
            }

            if($this->_checkPwd($password,$info['firstpwd'])){

                if($info['status']==0)
                {
                    echo"<script>alert('帐户还没有激活，请联系客服激活后再登录，谢谢！');location.href='/?u=".$username."'</script>";
                    die();
                }

                if($info['status']==2)
                {
                    echo"<script>alert('由于退单成功您的帐户已被冻结，如想继续加入本平台，请联系客服，谢谢！');location.href='/?u=".$username."'</script>";
                    die();
                }

                session('userid',$info['id']);
                session('username',$info['account']);
                session('name',$info['name']);
                session('phone',$info['phone']);
                session('email',$info['email']);
                session('status',$info['status']);//0  未开通，１已开通　２休眠　３冻结　
                session('price',$info['investmoney']);//投资额度
                session('txprice',$info['cashmoney']);//提现钱包
                session('yearprice',$info['yearmoney']);//半年钱包
                session('halfprice',$info['halfmoney']);//季度钱包
                session('ztprice',$info['zhitui']);//直推提现钱包
                session('logintime',time());//登录时间
                if($info['level']==0){
                    session('level','免费会员');
                }
                if($info['level']==1){
                    session('level','正式会员');
                }
                if($info['level']==2){
                    session('level','普通代理');
                }
                if($info['level']==3){
                    session('level','一级代理');
                }
                if($info['level']==4){
                    session('level','二级代理');
                }
                if($info['level']==5){
                    session('level','三级代理');
                }
                if($info['level']==6){
                    session('level','终极代理');
                }

                redirect('/user/');

            }else{
                echo"<script>alert('密码有误，请重新输入');location.href='/?u=".$username."'</script>";
                die();
            }

            
            exit;
        }else{

            echo"<script>alert('会员帐户为空，请重新输入');location.href='/?u=".$username."'</script>";
            die();
        }

        



        
    }

    // 验证码
    public function verify(){
      ob_clean();

        $config =    array(
            'fontSize'    =>    18,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'imageW'      =>    140,    //验证码宽度
            'imageH'      =>    35,    //验证码宽度
            'bg'          =>    array(243, 251, 254),    //验证码背景颜色 rgb数组设置，例如 array(243, 251, 254)
            'useCurve'  =>  false //// 是否画混淆曲线
        );
        $Verify =     new \Think\Verify($config);
        $Verify->entry();
    }

    //检验 验证码
    function check_verify($code, $id = ''){
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
    }

    /**
     * 密码加密
     */
    function md5pw($passwd)
    {
        $ph = new \Think\PasswordHash();
        return $ph->HashPassword($passwd);

    }

    /**
     * 检测密码是否正确
     * @param $getPwd
     * @param $dataPasswd
     */
    function _checkPwd($getPwd,$dataPasswd)
    {
        $ph = new \Think\PasswordHash();
        if($ph->CheckPassword($getPwd,$dataPasswd))
        {
            return true;
        }
        else
        {
            return false;
        }
    }


    public function zhmm(){


        $this->display();
    }

    public function mibao(){
        header("Content-Type: text/html;charset=utf-8");

        $account = I('post.account');
        if(!I('post.account')){

            die("<script>alert('请输入您要找回密码的账户名!');history.back(-1);</script>");

        }
        $user=M('account')->where(array('account' => $account))->find();
        if(empty($user)){
            die("<script>alert('您输入的账户名不存在，请重新输入!');history.back(-1);</script>");
        }

        if(empty($user['mbwt'])){
            die("<script>alert('您未设置密保问题，无法通过密保问题找回密码!');history.back(-1);</script>");
        }
        if(empty($user['mbdan'])){
            die("<script>alert('您未设置密保问题，无法通过密保问题找回密码!');history.back(-1);</script>");
        }

        $mbwt = I('post.mbwt');
        if($mbwt!=$user['mbwt']){
            die("<script>alert('您选择的密保问题不正确，请重新选择!');history.back(-1);</script>");
        }

        $mbdan = I('post.mbdan');
        if(!I('post.mbdan')){

            die("<script>alert('请输入密保答案!');history.back(-1);</script>");

        }
        if($mbdan!=$user['mbdan']){
            die("<script>alert('您输入的密保答案不正确，请重新输入!');history.back(-1);</script>");
        }

        session('xgmmid',$user['id']);



        echo"<script>alert('找回密码成功，请设置新登录密码!');location.href='/index/xgmm.html'</script>";


    }

    public function xgmm(){
        header("Content-Type: text/html;charset=utf-8");

        if(empty($_SESSION['xgmmid']))
        {
            unset($_SESSION['xgmmid']);  
            
            echo "<script>location.href='/'</script>";
            die();
        }


        $this->display();
    }

    public function szxmm(){
        header("Content-Type: text/html;charset=utf-8");

        if(!I('post.xmm')){

            die("<script>alert('请输入新密码!');history.back(-1);</script>");

        }
        if(!I('post.qrxmm')){

            die("<script>alert('请输入确认新密码!');history.back(-1);</script>");

        }
        $xmm=I('post.xmm');
        $qrxmm=I('post.qrxmm');
        if($xmm!=$qrxmm){

            die("<script>alert('输入的新密码和确认新密码不一致!');history.back(-1);</script>");

        }
        // $data['firstpwd']=md5(I('post.xmm'));
        $data['firstpwd']=$this->md5pw(I('post.xmm'));
        $user=M('account')->where(array('id' => $_SESSION['xgmmid']))->save($data);

        unset($_SESSION['xgmmid']);

        if($user){

            echo"<script>alert('设置成功!');location.href='/'</script>";
            
        }else{
            
            die("<script>alert('设置失败!');history.back(-1);</script>");
        }


    }

    public function register(){
        header("Content-Type: text/html;charset=utf-8");

        $tjr = I('get.tj');

        if(empty($tjr))
        { 
            echo "<script>location.href='/'</script>";
            die();
        }
        
        $this->tjr=$tjr;

        $adminbank=M('bank')->where(array('userid' => 0))->select();
        $this->assign('adminbank', $adminbank);

        $setting=M('setting')->where('id=1')->find();
        $this->assign('setting',$setting);
        


        $this->display();
    }

    public function add(){
        header("Content-Type: text/html;charset=utf-8");

        $yh=I('post.yh');
        if(!$yh){

            die("<script>alert('平台银行账户不能为空!');history.back(-1);</script>");

        }else{

            $bank=M('bank')->where(array('id' => $yh))->find();
            $data['pt_bank_name']=$bank['bank_name'];
            $data['pt_bank_username']=$bank['bank_username'];
            $data['pt_bank_num']=$bank['bank_num'];
            $data['pt_bank_zhi']=$bank['bank_zhi'];

        }

        // $data['parid'] = $_SESSION['userid'];//默认推荐人
        $data['status'] = 0;//默认未激活
        $data['zctime'] = time();//注册时间

        $data['account'] = I('post.account');
        if(empty($data['account'])){

            die("<script>alert('请填写会员账号!');history.back(-1);</script>");

        }
        $user=M('account')->where(array('account' => $data['account']))->find();
        if(!empty($user)){

            die("<script>alert('该会员账号已存在，请换一个试试!');history.back(-1);</script>");

        }
        $data['name'] = I('post.name');
        if(empty($data['name'])){

            die("<script>alert('请填写真实姓名!');history.back(-1);</script>");

        }
        $data['email'] = I('post.email');
        // if(empty($data['email'])){

        //     die("<script>alert('请填写邮箱!');history.back(-1);</script>");

        // }
        if(I('post.email')){
            // if (!preg_match ( ' /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$data['email'] )) {
            if (!preg_match ( ' /^\w+((-\w+)|(\.\w+))*\@[0-9a-zA-Z]+((-|\.)[0-9a-zA-Z]+)*\.[0-9a-zA-Z]+$/',I('post.email') )) {
                die("<script>alert('邮箱格式不对!');history.back(-1);</script>");
            }
        }
        $data['phone'] = I('post.phone');
        if(empty($data['phone'])){

            die("<script>alert('请填写手机号码!');history.back(-1);</script>");

        }
        if (I('post.phone')) {
            //正则表达式匹配
            if (!preg_match ( ' /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))\\d{8})$/',I('post.phone') )) {
                die("<script>alert('手机格式不对!');history.back(-1);</script>");
            }
        }
        $data['investmoney'] = I('post.investmoney');
        if(empty($data['investmoney'])){

            die("<script>alert('请填写投资金额!');history.back(-1);</script>");

        }
        // if(($data['investmoney']<1000) || ($data['investmoney']>30000) || ($data['investmoney'] % 1000 >0)){

        //     die("<script>alert('投资金额1000~30000,且必须是1000的倍数!');history.back(-1);</script>");

        // }
        $setting=M('setting')->where('id=1')->find();
        if(($data['investmoney']<$setting['min_money']) || ($data['investmoney']>$setting['max_money']) || ($data['investmoney'] % $setting['bei_money'] >0)){

            // die("<script>alert('投资金额1000~30000,且必须是1000的倍数!');history.back(-1);</script>");
            die("<script>alert('投资金额".$setting['min_money']."~".$setting['max_money'].",且必须是".$setting['bei_money']."的倍数!');history.back(-1);</script>");

        }
        if($data['investmoney']>0 && $data['investmoney']<5000){
            $data['level'] = 1;
        }
        if($data['investmoney']>=5000 && $data['investmoney']<10000){
            $data['level'] = 3;
        }
        if($data['investmoney']>=10000 && $data['investmoney']<20000){
            $data['level'] = 4;
        }
        if($data['investmoney']>=20000 && $data['investmoney']<=30000){
            $data['level'] = 5;
        }
        // if($data['investmoney']>=30000){
        //     $data['level'] = 6;
        // }
        // if($data['level']>1){
        //     $data['daitime'] = time();//成为代理的时间
        // }

        if(!I('post.firstpwd')){

            die("<script>alert('请填写一级密码!');history.back(-1);</script>");

        }
        if(!I('post.qr_firstpwd')){

            die("<script>alert('请填写确认一级密码!');history.back(-1);</script>");

        }
        $firstpwd = I('post.firstpwd');
        $qr_firstpwd = I('post.qr_firstpwd');

        if($firstpwd<>$qr_firstpwd){

            die("<script>alert('填写的一级密码和确认一级密码不一致!');history.back(-1);</script>");

        }
        $data['firstpwd'] = $this->md5pw(I('post.firstpwd'));

        
        if(!I('post.secpwd')){

            die("<script>alert('请填写二级密码!');history.back(-1);</script>");

        }
        if(!I('post.qr_secpwd')){

            die("<script>alert('请填写确认二级密码!');history.back(-1);</script>");

        }
        $secpwd = I('post.secpwd');
        $qr_secpwd = I('post.qr_secpwd');
        if($secpwd<>$qr_secpwd){

            die("<script>alert('填写的二级密码和确认二级密码不一致!');history.back(-1);</script>");

        }
        $data['secpwd'] = $this->md5pw(I('post.secpwd'));

        
        if(!I('post.thirpwd')){

            die("<script>alert('请填写三级密码!');history.back(-1);</script>");

        }
        if(!I('post.qr_thirpwd')){

            die("<script>alert('请填写确认三级密码!');history.back(-1);</script>");

        }
        $thirpwd = I('post.thirpwd');
        $qr_thirpwd = I('post.qr_thirpwd');
        if($thirpwd<>$qr_thirpwd){

            die("<script>alert('填写的三级密码和确认三级密码不一致!');history.back(-1);</script>");

        }
        $data['thirpwd'] = $this->md5pw(I('post.thirpwd'));

        $tjr = I('post.tjr');
        if(!I('post.tjr')){

            die("<script>alert('请填写推荐人账户名!');history.back(-1);</script>");

        }
        $tjruser=M('account')->where(array('account' => $tjr))->find();
        if(!$tjruser){

            die("<script>alert('填写的推荐人账户名不存在，请重新填写!');history.back(-1);</script>");

        }

        $data['parid'] = $tjruser['id'];
        $data['mbwt'] = I('post.mbwt');
        $data['mbdan'] = I('post.mbdan');
        if(!I('post.mbwt')){

            die("<script>alert('请选择密保问题!');history.back(-1);</script>");

        }
        if(!I('post.mbdan')){

            die("<script>alert('请填写密保答案!');history.back(-1);</script>");

        }
        if(!I('post.cart_id')){

            die("<script>alert('请填写身份证号!');history.back(-1);</script>");

        }
        $data['cart_id'] = I('post.cart_id');

        // if(!I('post.province')){

        //     die("<script>alert('请填写省·市·地区详细地址!');history.back(-1);</script>");

        // }
        $data['province'] = I('post.province');

        if(!I('post.bank_username')){

            die("<script>alert('请填写开户姓名!');history.back(-1);</script>");

        }
        $data['bank_username'] = I('post.bank_username');

        if(!I('post.bank_num')){

            die("<script>alert('请填写银行卡号!');history.back(-1);</script>");

        }
        $data['bank_num'] = I('post.bank_num');

        if(!I('post.bank_name')){

            die("<script>alert('请填写开户行!');history.back(-1);</script>");

        }
        $data['bank_name'] = I('post.bank_name');

        if(!I('post.bank_zhi')){

            die("<script>alert('请填写支行信息!');history.back(-1);</script>");

        }
        $data['bank_zhi'] = I('post.bank_zhi');

        $reg=M('account')->add($data);//生成新会员

        if($reg){
            echo"<script>alert('注册成功，等待激活!');location.href='/'</script>";
            
        }else{
            
            echo"<script>alert('注册失败!');location.href='/index/register?tj=".$tjr."'</script>";
        }


        
    }
























}